package com.lvyq.myplayer.base

import android.graphics.Color
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.lvyq.myplayer.R
 import org.jetbrains.anko.find

/**
 * 文件描述：BaseListFragment
 * 作者：lvyq
 * 创建时间：2025-02-25
 * 修改时间：2025-02-25
 */
abstract class BaseListFragment<RESPONSE ,ITEMBEAN,ITEMVIEW:View>:BaseFragment(), BaseView<RESPONSE> {


    lateinit var  recyclerView : RecyclerView
    lateinit var refresh_layout: SwipeRefreshLayout


    val adapter by lazy { getSpecialAdapter() }
    //获取适配器adapter
    abstract fun getSpecialAdapter():BaseListAdapter<ITEMBEAN,ITEMVIEW>

    val presenter by lazy { getSpecialPresenter() }
   abstract fun getSpecialPresenter():BaseListPresenter


    override fun initView(): View? {
        var view = View.inflate(context, R.layout.fragment_home, null)
        //从initView中获取recyclerView id，无法在initListener直接使用。不知原因
        recyclerView = view.findViewById<RecyclerView>(R.id.recyclerView)
        refresh_layout=view.find<SwipeRefreshLayout>(R.id.refresh_layout)
        return view
    }


    override fun initListener() {
        //初始化recyclerView
        //设置布局
        recyclerView.layoutManager= LinearLayoutManager(context)
        //适配
        recyclerView.adapter=adapter
        //设置下拉刷新颜色
        refresh_layout.setColorSchemeColors(Color.RED, Color.BLUE)
        //设置下拉监听
        refresh_layout.setOnRefreshListener {
            //刷新数据
            presenter.loadDatas()
        }
        //监听列表滑动
        recyclerView.addOnScrollListener(object: RecyclerView.OnScrollListener(){
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                //空闲状态
                if (newState== RecyclerView.SCROLL_STATE_IDLE){
                    //判断滑动到最后一条
                    var layoutManager = recyclerView.layoutManager
                    if (layoutManager is LinearLayoutManager){
                        //只有线性布局才有最后一条的概念
                        val manager=layoutManager
                        //获取最后条目
                        var lastVisibleItemPosition = manager.findLastVisibleItemPosition()
                        //无最新数据
                        if (lastVisibleItemPosition==adapter.totalCount){
                            return
                        }
                        if (lastVisibleItemPosition==adapter.itemCount-1){
                            //最后一条,加载更多数据
                            presenter.loadMore(adapter.itemCount)
                        }
                    }
                }
            }

        })
    }


    /**
     * 获取数据
     */
    override fun initData() {
        //获取数据
        presenter.loadDatas()
    }

    override fun onError(message: String) {
        myToast(message)
    }

    /**
     * 初始化数据
     */
    override fun loadSuccess(response: RESPONSE, totalCount:Int) {
        response?.let {
            //隐藏刷新
            refresh_layout.isRefreshing=false
            //更新数据
            adapter.updateList(getList(response),totalCount)
        }
    }

    /**
     * 加载更多
     */
    override fun loadMore(response: RESPONSE, totalCount:Int) {
        response?.let {
            //隐藏刷新
            refresh_layout.isRefreshing=false
            //更新数据
            adapter.loadMore(getList(response),totalCount)
        }
    }

    abstract fun getList(response: RESPONSE):List<ITEMBEAN>


}

